<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::CoreExtensions::Array::Conversions</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::CoreExtensions::Array::Conversions</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../../files/vendor/rails/activesupport/lib/active_support/core_ext/array/conversions_rb.html">
                vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000483">to_formatted_s</a>&nbsp;&nbsp;
      <a href="#M000481">to_param</a>&nbsp;&nbsp;
      <a href="#M000482">to_query</a>&nbsp;&nbsp;
      <a href="#M000480">to_sentence</a>&nbsp;&nbsp;
      <a href="#M000484">to_xml</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000483" class="method-detail">
        <a name="M000483"></a>

        <div class="method-heading">
          <a href="#M000483" class="method-signature">
          <span class="method-name">to_formatted_s</span><span class="method-args">(format = :default)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Converts a collection of elements into a formatted string by calling
<tt>to_s</tt> on all elements and joining them:
</p>
<pre>
  Blog.find(:all).to_formatted_s # =&gt; &quot;First PostSecond PostThird Post&quot;
</pre>
<p>
Adding in the <tt>:db</tt> argument as the format yields a prettier output:
</p>
<pre>
  Blog.find(:all).to_formatted_s(:db) # =&gt; &quot;First Post,Second Post,Third Post&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000483-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000483-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 62</span>
62:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_formatted_s</span>(<span class="ruby-identifier">format</span> = <span class="ruby-identifier">:default</span>)
63:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">format</span>
64:             <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:db</span>
65:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:empty?</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">empty?</span>
66:                 <span class="ruby-value str">&quot;null&quot;</span>
67:               <span class="ruby-keyword kw">else</span>
68:                 <span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">id</span> }.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;,&quot;</span>)
69:               <span class="ruby-keyword kw">end</span>
70:             <span class="ruby-keyword kw">else</span>
71:               <span class="ruby-identifier">to_default_s</span>
72:           <span class="ruby-keyword kw">end</span>
73:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000481" class="method-detail">
        <a name="M000481"></a>

        <div class="method-heading">
          <a href="#M000481" class="method-signature">
          <span class="method-name">to_param</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Calls <tt><a href="Conversions.html#M000481">to_param</a></tt> on all its
elements and joins the result with slashes. This is used by
<tt>url_for</tt> in Action Pack.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000481-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000481-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 33</span>
33:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_param</span>
34:           <span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_param</span> }.<span class="ruby-identifier">join</span> <span class="ruby-value str">'/'</span>
35:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000482" class="method-detail">
        <a name="M000482"></a>

        <div class="method-heading">
          <a href="#M000482" class="method-signature">
          <span class="method-name">to_query</span><span class="method-args">(key)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Converts an array into a string suitable for use as a URL query string,
using the given <tt>key</tt> as the param name.
</p>
<pre>
  ['Rails', 'coding'].to_query('hobbies') # =&gt; &quot;hobbies%5B%5D=Rails&amp;hobbies%5B%5D=coding&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000482-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000482-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 41</span>
41:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_query</span>(<span class="ruby-identifier">key</span>)
42:           <span class="ruby-identifier">prefix</span> = <span class="ruby-node">&quot;#{key}[]&quot;</span>
43:           <span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">to_query</span>(<span class="ruby-identifier">prefix</span>) }.<span class="ruby-identifier">join</span> <span class="ruby-value str">'&amp;'</span>
44:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000480" class="method-detail">
        <a name="M000480"></a>

        <div class="method-heading">
          <a href="#M000480" class="method-signature">
          <span class="method-name">to_sentence</span><span class="method-args">(options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Converts the array to a comma-separated sentence where the last element is
joined by the connector word. Options:
</p>
<ul>
<li><tt>:connector</tt> - The word used to join the last element in arrays with
two or more elements (default: &quot;and&quot;)

</li>
<li><tt>:skip_last_comma</tt> - Set to true to return &quot;a, b and c&quot;
instead of &quot;a, b, and c&quot;.

</li>
</ul>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000480-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000480-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 10</span>
10:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_sentence</span>(<span class="ruby-identifier">options</span> = {})          
11:           <span class="ruby-identifier">options</span>.<span class="ruby-identifier">assert_valid_keys</span>(<span class="ruby-identifier">:connector</span>, <span class="ruby-identifier">:skip_last_comma</span>, <span class="ruby-identifier">:locale</span>)
12:           
13:           <span class="ruby-identifier">default</span> = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'support.array.sentence_connector'</span><span class="ruby-value str">'support.array.sentence_connector'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>])
14:           <span class="ruby-identifier">default_skip_last_comma</span> = <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value str">'support.array.skip_last_comma'</span><span class="ruby-value str">'support.array.skip_last_comma'</span>, <span class="ruby-identifier">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:locale</span>])
15:           <span class="ruby-identifier">options</span>.<span class="ruby-identifier">reverse_merge!</span> <span class="ruby-identifier">:connector</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">default</span>, <span class="ruby-identifier">:skip_last_comma</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">default_skip_last_comma</span>
16:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:connector</span>] = <span class="ruby-node">&quot;#{options[:connector]} &quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:connector</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:connector</span>].<span class="ruby-identifier">strip</span> <span class="ruby-operator">==</span> <span class="ruby-value str">''</span>
17: 
18:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">length</span>
19:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">0</span>
20:               <span class="ruby-value str">&quot;&quot;</span>
21:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">1</span>
22:               <span class="ruby-keyword kw">self</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">to_s</span>
23:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">2</span>
24:               <span class="ruby-node">&quot;#{self[0]} #{options[:connector]}#{self[1]}&quot;</span>
25:             <span class="ruby-keyword kw">else</span>
26:               <span class="ruby-node">&quot;#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}&quot;</span>
27:           <span class="ruby-keyword kw">end</span>
28:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000484" class="method-detail">
        <a name="M000484"></a>

        <div class="method-heading">
          <a href="#M000484" class="method-signature">
          <span class="method-name">to_xml</span><span class="method-args">(options = {}) {|xml if block_given?| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns a string that represents this array in XML by sending <tt><a
href="Conversions.html#M000484">to_xml</a></tt> to each element. Active
Record collections delegate their representation in XML to this method.
</p>
<p>
All elements are expected to respond to <tt><a
href="Conversions.html#M000484">to_xml</a></tt>, if any of them does not an
exception is raised.
</p>
<p>
The root node reflects the class name of the first element in plural if all
elements belong to the same type and that&#8216;s not Hash:
</p>
<pre>
  customer.projects.to_xml

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;projects type=&quot;array&quot;&gt;
    &lt;project&gt;
      &lt;amount type=&quot;decimal&quot;&gt;20000.0&lt;/amount&gt;
      &lt;customer-id type=&quot;integer&quot;&gt;1567&lt;/customer-id&gt;
      &lt;deal-date type=&quot;date&quot;&gt;2008-04-09&lt;/deal-date&gt;
      ...
    &lt;/project&gt;
    &lt;project&gt;
      &lt;amount type=&quot;decimal&quot;&gt;57230.0&lt;/amount&gt;
      &lt;customer-id type=&quot;integer&quot;&gt;1567&lt;/customer-id&gt;
      &lt;deal-date type=&quot;date&quot;&gt;2008-04-15&lt;/deal-date&gt;
      ...
    &lt;/project&gt;
  &lt;/projects&gt;
</pre>
<p>
Otherwise the root element is &quot;records&quot;:
</p>
<pre>
  [{:foo =&gt; 1, :bar =&gt; 2}, {:baz =&gt; 3}].to_xml

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;records type=&quot;array&quot;&gt;
    &lt;record&gt;
      &lt;bar type=&quot;integer&quot;&gt;2&lt;/bar&gt;
      &lt;foo type=&quot;integer&quot;&gt;1&lt;/foo&gt;
    &lt;/record&gt;
    &lt;record&gt;
      &lt;baz type=&quot;integer&quot;&gt;3&lt;/baz&gt;
    &lt;/record&gt;
  &lt;/records&gt;
</pre>
<p>
If the collection is empty the root element is &quot;nil-classes&quot; by
default:
</p>
<pre>
  [].to_xml

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;nil-classes type=&quot;array&quot;/&gt;
</pre>
<p>
To ensure a meaningful root element use the <tt>:root</tt> option:
</p>
<pre>
  customer_with_no_projects.projects.to_xml(:root =&gt; &quot;projects&quot;)

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;projects type=&quot;array&quot;/&gt;
</pre>
<p>
By default root children have as node name the one of the root
singularized. You can change it with the <tt>:children</tt> option.
</p>
<p>
The <tt>options</tt> hash is passed downwards:
</p>
<pre>
  Message.all.to_xml(:skip_types =&gt; true)

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;messages&gt;
    &lt;message&gt;
      &lt;created-at&gt;2008-03-07T09:58:18+01:00&lt;/created-at&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;name&gt;1&lt;/name&gt;
      &lt;updated-at&gt;2008-03-07T09:58:18+01:00&lt;/updated-at&gt;
      &lt;user-id&gt;1&lt;/user-id&gt;
    &lt;/message&gt;
  &lt;/messages&gt;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000484-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000484-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/array/conversions.rb, line 150</span>
150:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_xml</span>(<span class="ruby-identifier">options</span> = {})
151:           <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Not all elements respond to to_xml&quot;</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">all?</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-identifier">:to_xml</span> }
152: 
153:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root</span>]     <span class="ruby-operator">||=</span> <span class="ruby-identifier">all?</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-identifier">first</span>.<span class="ruby-identifier">class</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">first</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">!=</span> <span class="ruby-value str">&quot;Hash&quot;</span> } <span class="ruby-operator">?</span> <span class="ruby-identifier">first</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">underscore</span>.<span class="ruby-identifier">pluralize</span> <span class="ruby-operator">:</span> <span class="ruby-value str">&quot;records&quot;</span>
154:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:children</span>] <span class="ruby-operator">||=</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root</span>].<span class="ruby-identifier">singularize</span>
155:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:indent</span>]   <span class="ruby-operator">||=</span> <span class="ruby-value">2</span>
156:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>]  <span class="ruby-operator">||=</span> <span class="ruby-constant">Builder</span><span class="ruby-operator">::</span><span class="ruby-constant">XmlMarkup</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">:indent</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:indent</span>])
157: 
158:           <span class="ruby-identifier">root</span>     = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:root</span>).<span class="ruby-identifier">to_s</span>
159:           <span class="ruby-identifier">children</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:children</span>)
160: 
161:           <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:dasherize</span>) <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:dasherize</span>]
162:             <span class="ruby-identifier">root</span> = <span class="ruby-identifier">root</span>.<span class="ruby-identifier">dasherize</span>
163:           <span class="ruby-keyword kw">end</span>
164: 
165:           <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>].<span class="ruby-identifier">instruct!</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:skip_instruct</span>)
166: 
167:           <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">merge</span>({ <span class="ruby-identifier">:root</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">children</span> })
168: 
169:           <span class="ruby-identifier">xml</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>]
170:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">empty?</span>
171:             <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">tag!</span>(<span class="ruby-identifier">root</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:skip_types</span>] <span class="ruby-operator">?</span> {} <span class="ruby-operator">:</span> {<span class="ruby-identifier">:type</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;array&quot;</span>})
172:           <span class="ruby-keyword kw">else</span>
173:             <span class="ruby-identifier">xml</span>.<span class="ruby-identifier">tag!</span>(<span class="ruby-identifier">root</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:skip_types</span>] <span class="ruby-operator">?</span> {} <span class="ruby-operator">:</span> {<span class="ruby-identifier">:type</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;array&quot;</span>}) {
174:               <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">xml</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
175:               <span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_xml</span>(<span class="ruby-identifier">opts</span>.<span class="ruby-identifier">merge</span>({ <span class="ruby-identifier">:skip_instruct</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span> })) }
176:             }
177:           <span class="ruby-keyword kw">end</span>
178:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>